﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLBH.Base;
using QLBH.Model.Controller;
using QLBH.StandardUI;
using QLBH.ModelInterface;

namespace QLBH.Category
{
    public partial class AddNganhHang : Form
    {
        public eDlgType DlgType { set; get; }
        private IQLNganhHangModel model;

        public IQLNganhHangModel Model
        {
            get { return model; }
            set { model = value; }
        }
        public AddNganhHang()
        {
            InitializeComponent();

            model = ModelFactory.ModelFactory.GetQLNganhHangModelInstance();
            model.ID = Guid.NewGuid();
            model.MaNH = CodeHelper.GetNewCode();
            model.NgayTao = DateTime.Now;

            tbxMaNH.Text = model.MaNH;
        }

        private void AddNganhHangDlg_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                this.DialogResult = DialogResult.Cancel;
                this.Close();
            }
            else if (e.Control && e.KeyCode == Keys.S)
            {
                //Ctrl+S
                btnSave_Click(sender, e);
            }
        }

        private void AddNganhHangDlg_Load(object sender, EventArgs e)
        {
            if (DlgType==eDlgType.EDIT)
            {
                this.Text = "Xửa thông tin ngành hàng";
                UpdateModeltoControl();
            }
            else
            {
                this.Text = "Thêm mới ngành hàng";
                tbxMaNH.Text = model.MaNH;
                label6.Text = model.NgayTao.ToShortDateString();
            }
        }
        private void UpdateModeltoControl()
        {
            tbxMaNH.Text = model.MaNH;
            tbxTenNH.Text = model.TenNH;
            rtbMoTa.Text = model.Mota;
            rtbGhiChu.Text = model.GhiChu;
            label6.Text = model.NgayTao.ToShortDateString();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (CheckValidValueInput())
            {
                try
                {
                    UpdateModelFromControl();
                    if (DlgType == eDlgType.ADD)
                    {
                        var item=NganhHangController.GetInstance().Add(model.ToEntity());
                        model.InitFromEntity(item);
                    }
                    else
                    {
                        NganhHangController.GetInstance().Edit(model.ToEntity());
                    }
                    DialogResult = DialogResult.OK;
                    this.Close();
                }
                catch (System.Exception ex)
                {
                    ShowMessage.Instance.ShowError(ex.Message);
                    return;
                }
            }
        }

        private void UpdateModelFromControl()
        {
            model.MaNH = tbxMaNH.Text;
            model.TenNH = tbxTenNH.Text;
            model.Mota = rtbMoTa.Text;
            model.GhiChu = rtbGhiChu.Text;
        }
        private bool CheckValidValueInput()
        {
            try
            {
                DataInputHelper.CheckTextData(tbxMaNH.Text, "Mã ngành hàng",14);
            }
            catch (System.Exception ex)
            {
                ShowMessage.Instance.ShowError(ex.Message);
                tbxMaNH.SelectAll();
                tbxMaNH.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckTextData(tbxTenNH.Text, "Tên ngành hàng",50);
            }
            catch (System.Exception ex)
            {
                ShowMessage.Instance.ShowError(ex.Message);
                tbxTenNH.SelectAll();
                tbxTenNH.Focus();
                return false;
            }
            return true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}
